package com.woniuxy.qiantai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.qiantai.entity.Order;
import com.woniuxy.qiantai.entity.Shop;
import com.woniuxy.qiantai.mapper.OrderMapper;
import com.woniuxy.qiantai.mapper.ShopMapper;
import com.woniuxy.qiantai.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.qiantai.utils.List2PageUtils;
import com.woniuxy.qiantai.vo.ShopAndTypeES;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    @Autowired
    ShopMapper shopMapper;

    @Autowired
    ElasticsearchRepository elasticsearchRepository;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public Shop selectShopInfo(Integer shopId) {

        return shopMapper.selectById(shopId);
    }

//    @Override
//    public Page<Shop> getAllShop(Integer currentPage, Integer pageSize) {
//
//        Page<Shop> page = new Page<>(currentPage, pageSize);
//        Page<Shop> shopPage = shopMapper.selectPage(page, null);
//
//        return shopPage;
//    }


//    @Override
//    public Page<Shop> selectShopByType(String typeName, Integer currentPage,
//                                       Integer pageSize) {
//        PageHelper.startPage(currentPage,pageSize);
//        Page<Shop> shopPage = shopMapper.selectShopByType(new Page<>(currentPage, pageSize),
//                typeName);
//        return shopPage;
//    }

//    @Override
//    public PageInfo<Shop> searchShopByES(Integer currentPage, Integer pageSize,
//                                         String keyword) {
//
//        //构建查询条件
//        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//
//        //query
//        nativeSearchQueryBuilder.withQuery(QueryBuilders.multiMatchQuery(keyword, "name",
//                "typeName", "detail")).addAggregation(
//                AggregationBuilders.terms("shopIds").field("shopId"));
//
//        AggregatedPage<ShopAndTypeES> aggPage =
//                (AggregatedPage<ShopAndTypeES>) this.elasticsearchRepository.search(nativeSearchQueryBuilder.build());
//        // 3、解析
//        ParsedStringTerms agg = (ParsedStringTerms) aggPage.getAggregation("shopIds");
//        List<? extends Terms.Bucket> buckets = agg.getBuckets();
//        List<Shop> shopList = new ArrayList<>();
//        // 3.3、遍历
//        for (Terms.Bucket bucket : buckets) {
//            // 3.4、获取桶中的key，即品牌名称
//            //  System.out.println((Integer)bucket.getKeyAsNumber().intValue());
//            // 3.5、获取桶中的文档数量
//            //System.out.println(bucket.getDocCount());
//            Shop shop = new Shop();
//            shop.setId(bucket.getKeyAsNumber().intValue());
//            shopList.add(shop);
//        }
//        for (Shop shop : shopList) {
//            int currentIndex = shopList.indexOf(shop);
//            shop = shopMapper.selectById(shop.getId());
//            shopList.set(currentIndex, shop);
//        }
//        PageInfo<Shop> shopPageInfo = new PageInfo<>();
//        List<Shop> shopToAddList = new ArrayList<>();
//        List2PageUtils list2PageUtils = new List2PageUtils();
//
//        return list2PageUtils.pageInfo(shopList, shopToAddList, shopPageInfo, currentPage,
//                pageSize);
//    }

    @Override
    public String getShopName(Integer orderId) {
        Order order = orderMapper.selectById(orderId);
        Shop shop = shopMapper.selectById(order.getShopId());
        return shop.getName();
    }

}
